﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using wsRK.AppUser;
using wsRK.Web;

namespace nkstdgia.main.membership
{
    public partial class Role : WebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this._bindData();
            }
        }

        private void _bindData()
        {
            List<string> lsource = RoleDataservice.getAllRoles().ToList();
            if (User.IsInRole("manager"))
            {
                lsource.Remove("administrator");
            }
            AvailableRoles.DataSource = lsource;
            AvailableRoles.DataBind();
        }

        protected void lbtnAddRole_Click(object sender, EventArgs e)
        {
            try
            {
                Roles.CreateRole(txtRoleName.Text);
                this._bindData();
            }
            catch (Exception)
            {
            }
        }

        protected void lbtnDeleteRole_Click(object sender, EventArgs e)
        {
            try
            {
                if (AvailableRoles.SelectedValue != "administrator"
                    && AvailableRoles.SelectedValue != "manager"
                      && AvailableRoles.SelectedValue != "registereduser")
                {
                    if (Roles.DeleteRole(AvailableRoles.SelectedValue))
                    {
                        AvailableRoles.DataSource = RoleDataservice.getAllRoles(); ;
                        AvailableRoles.DataBind();
                        base.showMasterMessage("Xóa thành công");
                    }
                    else
                    {
                        base.showMasterMessage("Có lỗi xảy ra, nhóm đang được sử dụng");
                    }
                }
                else
                {
                    base.showMasterMessage("Không thể xóa nhóm mặc định");
                }
            }
            catch (Exception)
            {
            }
        }
    }
}